package com.atguigu.tabletest

import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api.scala._
import org.apache.flink.table.api.{DataTypes, Table}
import org.apache.flink.table.descriptors.{Csv, FileSystem, Schema}


/**
 *
 * @description: dataStream转成table 输出到外部系统
 * @time: 2021-03-15 15:35
 * @author: baojinlong
 **/
object Example03 {
  def main(args: Array[String]): Unit = {
    val environment: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    // 设置并行度
    environment.setParallelism(1)

    // 创建表的执行环境
    val tableEnv: StreamTableEnvironment = StreamTableEnvironment.create(environment)
    // 读取数据 文件读入 文件写出
    // 连接外部系统,读取文件注册表
    val filePath: String = "E:/big-data/FlinkTutorial/src/main/resources/sensor.data"
    tableEnv
      .connect(new FileSystem().path(filePath))
      .withFormat(new Csv())
      .withSchema(
        new Schema()
          .field("id", DataTypes.STRING)
          .field("timestamp", DataTypes.BIGINT)
          .field("temperature", DataTypes.DOUBLE))
      .createTemporaryTable("inputTable")

    // 进行转换操作
    val sensorTable: Table = tableEnv.from("inputTable")
    val resultTable: Table = sensorTable
      .select('id, 'temperature)
      .filter('id === "sensor_1")
    // 聚合转换
    val aggTable: Table = sensorTable
      .groupBy("id") // 基于id分组
      .select('id, 'id.count as 'count)


    // 输出到文件
    val fileOutPath: String = "E:/big-data/FlinkTutorial/src/main/resources/sensorOut.data"
    tableEnv
      .connect(new FileSystem().path(fileOutPath))
      .withFormat(new Csv())
      .withSchema(
        new Schema()
          .field("id", DataTypes.STRING)
          .field("timestamp", DataTypes.BIGINT)
          .field("temperature", DataTypes.DOUBLE))
      .createTemporaryTable("outputTable")

    resultTable.insertInto("outputTable")
    // 注意aggTable不能写入到文件中.以为他是追加流,涉及到更新模式,追加,撤回,更新插入


    resultTable.toAppendStream[(String, Double)].print("result")
    // 注意聚合后的结果比较特殊不能直接输出
    aggTable.toRetractStream[(String, Long)].print("agg")

    environment.execute("table sql api")
  }

}
